#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <istream>
#include <vector>
using ll = int64_t;
#define printf
const ll inf=1e9+7;
ll n,m,ans=inf;
std::vector<ll> a,b,aa,bb,mm,lmax,rmin;

#define pa(a)do {\
        printf(#a"[]: ");\
        for(ll i=1;i<=n;i++){\
            printf("%lld ",a[i]);\
        }\
        printf("\n");\
    }while(0)

int main(){
    std::iostream::sync_with_stdio(false);
    std::cin.tie(nullptr);

    std::cin>>n>>m;
    a.resize(n+1);
    aa.resize(n+1);
    b.resize(n+1);
    bb.resize(n+2);
    mm.resize(n+2,inf);
    lmax.resize(n+2);
    rmin.resize(n+2,inf);

    for(ll i=1;i<=n;i++)std::cin>>a[i];
    for(ll i=1;i<=n;i++)std::cin>>b[i];

    for(ll i=n;i>=1;i--){
        mm[i]=std::min(mm[i+1],a[i]);
    }
    for(ll i=1;i<=n;i++){
        lmax[i]=std::max(lmax[i-1],b[i]);
    }
    for(ll i=n;i>=1;i--){
        rmin[i]=std::min(rmin[i+1],b[i]);
    }
    ll nmin=inf;
    for(ll i=1;i<=n;i++){
        nmin=std::min(nmin,b[i]);
        aa[i]=std::min(nmin,mm[i+1]);
    }
    mm.assign(n+2,-inf);
    for(ll i=1;i<=n;i++){
        mm[i]=std::max(mm[i-1],a[i]);
    }
    ll nmax=-inf;
    for(ll i=n;i>=1;i--){
        nmax=std::max(nmax,b[i]);
        bb[i]=std::max(nmax,mm[i-1]);
    }
    
    for(ll i=0;i<=m;i++){
        ans=std::min(ans,std::max(bb[n-(m-i)+1],lmax[i])-std::min(aa[i],rmin[n-(m-i)+1]));
    }
    pa(aa);
    pa(bb);
    pa(lmax);
    pa(rmin);
    std::cout<<ans<<"\n";
}